from mongoengine import Document, StringField, IntField, DateTimeField
from datetime import datetime

class AdReward(Document):
    # 假设 openid 是唯一的
    openid = StringField(required=True)  # 与User表的openid关联
    ar_type = StringField(required=True)  # 道具类型
    quantity = IntField(default=0)  # 道具数量
    created_at = DateTimeField(default=datetime.now)
    
    
    def save(self, *args, **kwargs):
        if not self.updated_at:  # 如果 updated_at 没有设置，则设置为创建时间
            self.updated_at = datetime.now()
        else:  # 否则更新为当前时间
            self.updated_at = datetime.now()
        super(User, self).save(*args, **kwargs)
        
